package src.main.其他模式.空对象模式;

public class NullPatternDemo {
    public static void main(String[] args) {

        AbstractCustomer customer1 = CustomerFactory.getCustomer("小明");
        AbstractCustomer customer2 = CustomerFactory.getCustomer("小白");
        AbstractCustomer customer3 = CustomerFactory.getCustomer("鸣人");
        AbstractCustomer customer4 = CustomerFactory.getCustomer("Laura");
        AbstractCustomer customer5 = CustomerFactory.getCustomer("老李");

        System.out.println("Customers");
        System.out.println(customer1.getName());
        System.out.println("是否为空："+customer1.isNil());
        System.out.println(customer2.getName());
        System.out.println("是否为空："+customer2.isNil());
        System.out.println(customer3.getName());
        System.out.println("是否为空："+customer3.isNil());
        System.out.println(customer4.getName());
        System.out.println("是否为空："+customer4.isNil());
        System.out.println(customer5.getName());
        System.out.println("是否为空："+customer5.isNil());
    }
}

abstract class AbstractCustomer {
    protected String name;
    public abstract boolean isNil();
    public abstract String getName();
}
/**对象非空时返回*/
class RealCustomer extends AbstractCustomer {
    public RealCustomer(String name) {this.name = name;}
    @Override
    public String getName() {return name;}
    @Override
    public boolean isNil() {return false;}
}
/**对象为空时返回*/
class NullCustomer extends AbstractCustomer {
    @Override
    public String getName() {return "没有客户信息";}
    @Override
    public boolean isNil() {return true;}
}
/**使用 CustomerFactory，基于客户传递的名字
 * 来获取 RealCustomer 或 NullCustomer 对象
 * 相当于中转类*/
class CustomerFactory {
    public static final String[] names = {"小明", "小白", "鸣人"};
    public static AbstractCustomer getCustomer(String name){
        for (int i = 0; i < names.length; i++) {
            if (names[i].equalsIgnoreCase(name)){
                return new RealCustomer(name);
            }
        }
        return new NullCustomer();
    }
}